<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * AuthRule: Administrator
 * Date: 2020/3/14
 * Time: 17:18
 */
namespace app\lnadmin\controller;
use app\core\InitController;
use think\facade\View;
use think\facade\Db;
use app\lnadmin\model\admin\AuthRule as AuthRuleModel;
use app\lnadmin\model\admin\AuthGroup as AuthGroupModel;

class AuthRule extends InitController
{

    protected function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 角色管理
     * @return mixed
     */
    public function list()
    {
        return $this->fetch('authRule');
    }

    public function index_json()
    {
        $model = new AuthRuleModel();
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            $count = $model->count();
            $list  = $model->order(['weigh'=>'desc'])->field(['id','pid','url','name as title','icon as icons','weigh','status','ismenu as type','openType'])->select();
            $pidMenuList = $this->dropTree($list);
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $pidMenuList,
            ];
            return json($data);
        }
       
    }
    /**
     * 添加新的角色json_encode($arrayName);
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();  
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'authRule');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = AuthRuleModel::create($param);
            if ($result == true) {
            add_admin_log('add', '菜单栏目');// 记录行为日志
                return $this->success('', 0,'添加成功',url('authRule/rule'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        $authRule = AuthRuleModel::where(['status' => 1])->order('weigh asc')->select();
        return $this->fetch('addRule', ['authRule' => list_to_level($authRule)]);
    }

    /**
     * 修改角色
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'authRule');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = AuthRuleModel::update($param,['id'=>$param['id']]);
            if ($result == true) {
            add_admin_log('edit', '菜单栏目');// 记录行为日志
                return $this->success('', 0,'修改成功',url('authRule/rule'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        $authRule = AuthRuleModel::where(['status' => 1])->order('weigh asc')->select();
        return $this->fetch('saveRule', [
            'data' => AuthRuleModel::where('id', input('id'))->find(),
            'authRule' => list_to_level($authRule)
        ]);
    }

    /**
     * 删除角色
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            AuthRuleModel::where('pid', input('id'))->count() && $this->error('请先删除子节点');
            AuthRuleModel::destroy($param['id']);
            add_admin_log('del', '菜单栏目');// 记录行为日志
            $this->success('删除成功');
        }
    }
    
}
